package com.xbongbong.pro.message.help;/*/*/

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.message.domain.entity.PushSetEntity;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.pojo.IdNamePropertyPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.JsonHelperUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.Entity;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.enums.PushObjectTypeEnum;
import com.xbongbong.pro.message.enums.PushParentTypeEnum;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.factory.MessageSetServiceFactory;
import com.xbongbong.pro.message.model.PushSetModel;
import com.xbongbong.pro.message.pushset.CluePushSet;
import com.xbongbong.pro.message.pushset.CommunicatePushSet;
import com.xbongbong.pro.message.pushset.ContractPushSet;
import com.xbongbong.pro.message.pushset.CustomerContactPushSet;
import com.xbongbong.pro.message.pushset.CustomerPushSet;
import com.xbongbong.pro.message.pushset.DistributorCommunicatePushSet;
import com.xbongbong.pro.message.pushset.DistributorContactPushSet;
import com.xbongbong.pro.message.pushset.DistributorPushSet;
import com.xbongbong.pro.message.pushset.InvoicePushSet;
import com.xbongbong.pro.message.pushset.InvoiceRedPushSet;
import com.xbongbong.pro.message.pushset.OpportunityPushSet;
import com.xbongbong.pro.message.pushset.OrderPushSet;
import com.xbongbong.pro.message.pushset.PayPlanPushSet;
import com.xbongbong.pro.message.pushset.PaySheetPrepaySet;
import com.xbongbong.pro.message.pushset.PaySheetPushSet;
import com.xbongbong.pro.message.pushset.PaySheetRedPrepaySet;
import com.xbongbong.pro.message.pushset.PaySheetRedPushSet;
import com.xbongbong.pro.message.pushset.PaymentPlanPushSet;
import com.xbongbong.pro.message.pushset.PaymentSheetBadPushSet;
import com.xbongbong.pro.message.pushset.PaymentSheetPrepaymentSet;
import com.xbongbong.pro.message.pushset.PaymentSheetPushSet;
import com.xbongbong.pro.message.pushset.PaymentSheetRedPrepaymentSet;
import com.xbongbong.pro.message.pushset.PaymentSheetRedPushSet;
import com.xbongbong.pro.message.pushset.PurchaseInvoicePushSet;
import com.xbongbong.pro.message.pushset.PurchaseInvoiceRedPushSet;
import com.xbongbong.pro.message.pushset.QuotationPushSet;
import com.xbongbong.pro.message.pushset.SignInPushSet;
import com.xbongbong.pro.message.pushset.WorkOrderPushSet;
import com.xbongbong.pro.message.pushset.WorkReportPushSet;
import com.xbongbong.pro.message.service.MessageSetService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.CustomerStatementStatisticsEntity;
import com.xbongbong.saas.domain.entity.DistributorAccountEntity;
import com.xbongbong.saas.domain.entity.InvoiceRelationshipEntity;
import com.xbongbong.saas.domain.entity.PurchaseInvoiceRelationshipEntity;
import com.xbongbong.saas.domain.entity.SignInEntity;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.domain.entity.WorkReportCcEntity;
import com.xbongbong.saas.domain.entity.WorkReportSetEntity;
import com.xbongbong.saas.domain.entity.ext.ClueEntityExt;
import com.xbongbong.saas.domain.entity.ext.ContactEntityExt;
import com.xbongbong.saas.domain.entity.ext.ContractEntityExt;
import com.xbongbong.saas.domain.entity.ext.CustomerCommunicateEntityExt;
import com.xbongbong.saas.domain.entity.ext.CustomerEntityExt;
import com.xbongbong.saas.domain.entity.ext.InvoiceEntityExt;
import com.xbongbong.saas.domain.entity.ext.OpportunityEntityExt;
import com.xbongbong.saas.domain.entity.ext.PayPlanEntityExt;
import com.xbongbong.saas.domain.entity.ext.PayPlanSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PurchaseEntityExt;
import com.xbongbong.saas.domain.entity.ext.PurchaseInvoiceEntityExt;
import com.xbongbong.saas.domain.entity.ext.QuotationEntityExt;
import com.xbongbong.saas.domain.entity.ext.WorkReportEntityExt;
import com.xbongbong.saas.enums.CustomerStatementStatisticDistributorEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.business.ContractEnum;
import com.xbongbong.saas.enums.business.CustomerCommunicateEnum;
import com.xbongbong.saas.enums.business.DistributorCommunicateEnum;
import com.xbongbong.saas.enums.business.DistributorContactEnum;
import com.xbongbong.saas.enums.business.OrderEnum;
import com.xbongbong.saas.enums.business.PayPlanEnum;
import com.xbongbong.saas.enums.business.PaySheetEnum;
import com.xbongbong.saas.enums.business.PaymentEnum;
import com.xbongbong.saas.enums.business.PaymentSheetEnum;
import com.xbongbong.saas.enums.business.PurchaseEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import com.xbongbong.saas.enums.business.WorkReportFieldEnum;
import com.xbongbong.saas.enums.dictionary.DistributorRoleEnum;
import com.xbongbong.saas.model.ClueUserModel;
import com.xbongbong.saas.model.ContractModel;
import com.xbongbong.saas.model.ContractUserModel;
import com.xbongbong.saas.model.CustomerUserModel;
import com.xbongbong.saas.model.DistributorAccountModel;
import com.xbongbong.saas.model.InvoiceRelationshipModel;
import com.xbongbong.saas.model.OpportunityUserModel;
import com.xbongbong.saas.model.PaymentModel;
import com.xbongbong.saas.model.PurchaseInvoiceRelationshipModel;
import com.xbongbong.saas.model.PurchaseModel;
import com.xbongbong.saas.model.PurchaseUserModel;
import com.xbongbong.saas.model.QuotationUserModel;
import com.xbongbong.saas.model.SupplierUserModel;
import com.xbongbong.saas.model.WorkOrderCcModel;
import com.xbongbong.saas.model.WorkOrderFlowUserModel;
import com.xbongbong.saas.model.WorkReportCcModel;
import com.xbongbong.saas.model.WorkReportSetModel;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserDepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.DepartmentModel;
import com.xbongbong.sys.model.UserDepartmentModel;
import com.xbongbong.sys.model.UserModel;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 推送相关helper
 * @author 魏荣杰
 * @date 2019/2/18 9:20
 * @since v1.0
 * @version v1.0
 */
@Component
public class PushHelper {
    private static final Logger LOG = LoggerFactory
            .getLogger(PushHelper.class);

    @Resource
    private PushSetModel pushSetModel;
    @Resource
    private CustomerUserModel customerUserModel;
    @Resource
    private OpportunityUserModel opportunityUserModel;
    @Resource
    private ContractModel contractModel;
    @Resource
    private PaymentModel paymentModel;
    @Resource
    private ContractUserModel contractUserModel;
    @Resource
    private WorkReportCcModel workReportCcModel;
    @Resource
    private WorkOrderFlowUserModel workOrderFlowUserModel;
    @Resource
    private WorkOrderCcModel workOrderCcModel;
    @Resource
    private InvoiceRelationshipModel invoiceRelationshipModel;
    @Resource
    private UserModel userModel;
    @Resource
    private DepartmentModel departmentModel;
    @Resource
    private UserDepartmentModel userDepartmentModel;
    @Resource
    private QuotationUserModel quotationUserModel;
    @Resource
    private PurchaseModel purchaseModel;
    @Resource
    private PurchaseUserModel purchaseUserModel;
    @Resource
    private SupplierUserModel supplierUserModel;
    @Resource
    private PurchaseInvoiceRelationshipModel purchaseInvoiceRelationshipModel;
    @Resource
    private ClueUserModel clueUserModel;
    @Resource
    private WorkReportSetModel workReportSetModel;
    @Resource
    private DistributorAccountModel distributorAccountModel;
    @Resource
    private MessageSetServiceFactory messageSetServiceFactory;


    /**
     * 根据设置获取推送对象人员id
     *
     * @param corpid        公司id
     * @param type          消息类型 与PushTypeEnum的code对应
     * @param subType       消息子类型 与PushTypeEnum的subCode对应
     * @param loginUser     用户entity
     * @param entity        业务entity
     * @author GuoJun.HU
     * @since V1.0
     * @return List
     * @date 2019/4/17 11:27
     */
    public List<String> getUserIdListByPushSetRule(String corpid, Integer type, Integer subType, UserVO loginUser, Entity entity) throws XbbException{
        UserEntity userEntity = new UserEntity();
        BeanUtil.copyProperties(loginUser, userEntity);
        return getUserIdListByPushSetRule(corpid, type, subType, userEntity, entity);
    }

    /**
     * Description 根据设置获取推送对象人员id
     * @param corpid 公司id
     * @param type 消息类型 与PushTypeEnum的code对应
     * @param subType 消息子类型 与PushTypeEnum的subCode对应
     * @param userEntity 用户entity
     * @param entity 业务entity
     * @return java.util.List<java.lang.String> 用户id
     * @author 魏荣杰
     * @date 2018/8/9 19:59
     * @since v3.26
     * Modified by:
     */
    public List<String> getUserIdListByPushSetRule(String corpid, Integer type, Integer subType, UserEntity userEntity, Entity entity) throws XbbException{
        if (entity == null || StringUtil.isEmpty(corpid) || type == null || subType == null) {
            return new ArrayList<>();
        }
        try{
            PushParentTypeEnum pushParentTypeEnum = PushParentTypeEnum.getByCode(type);
            if (Objects.nonNull(pushParentTypeEnum) && !Objects.equals(pushParentTypeEnum, PushParentTypeEnum.UNKNOWN)){
                XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(pushParentTypeEnum.getParentCode());
                MessageSetService abstractMessageSetService = messageSetServiceFactory.getMessageSetService(xbbRefTypeEnum.getAlias());
                return abstractMessageSetService.getUserIdListByPushSetRule(corpid, type, subType, userEntity, entity);
            }
        }catch (Exception e){
            LOG.error("com.xbongbong.pro.message.PushHelper.getUserIdListByPushSetRule fail", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001.getCode(),SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        List<String> userIds = new ArrayList<>();
        /*if (Objects.equals(PushTypeEnum.STATEMENT_GENERATE_PUSH.getCode(), type)) {
            userIds = getStatementUserIdList(corpid, subType, userEntity, entity);
        }*/
        Set<String> userIdsSet = new HashSet<>(userIds);
        userIds = new ArrayList<>(userIdsSet);
        return userIds;
    }

    private List<String> getStatementUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        List<String> userIdList = new ArrayList<>();
        // 经销商负责人、协同人
        CustomerStatementStatisticsEntity statementEntity = (CustomerStatementStatisticsEntity) entity;
        long customerId = statementEntity.getData().getLongValue(CustomerStatementStatisticDistributorEnum.CUSTOMER_ID.getAttr());
        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
        userIdList.removeIf(item -> Objects.equals(item, userEntity.getUserId()));
        return userIdList;
    }

    private List<String> getOrderUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        ContractEntityExt contractEntity = (ContractEntityExt) entity;
        OrderPushSet contractPushSet = new OrderPushSet();
        PushSetEntity pushSetEntity = contractPushSet.getPushSet(corpid, subType, pushSetModel);
        Long contractId = contractEntity.getId();

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (contractId != null && contractId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractEntity.getId(), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractEntity.getId(), 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.ORDER_SIGNING.getAlias())) {
                            JSONObject contractDataObject = JsonHelperUtil.getJSONObject(contractEntity.getData());
                            result.add(contractDataObject.getString(OrderEnum.SIGN_PERSON.getAttr()));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据客户推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getCustomerUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> userIdList = new HashSet<>();
        CustomerEntityExt customerEntity = (CustomerEntityExt) entity;
        Long customerId = customerEntity.getId();
        CustomerPushSet customerPushSet = new CustomerPushSet();
        PushSetEntity pushSetEntity = customerPushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (customerId != null && customerId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据经销商推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getDistributorUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> userIdList = new HashSet<>();
        CustomerEntityExt customerEntity = (CustomerEntityExt) entity;
        Long customerId = customerEntity.getId();
        DistributorPushSet distributorPushSet = new DistributorPushSet();
        PushSetEntity pushSetEntity = distributorPushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (customerId != null && customerId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据线索推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author youli.chen
     * @date 2020/12/25 14:20
     * @since v1.0
     */
    private List<String> getClueUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> userIdList = new HashSet<>();
        ClueEntityExt clueEntityExt = (ClueEntityExt) entity;
        Long clueId = clueEntityExt.getId();
        CluePushSet cluePushSet = new CluePushSet();
        PushSetEntity pushSetEntity = cluePushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (clueId != null && clueId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            userIdList.addAll(clueUserModel.getClueUserIds(corpid, Collections.singletonList(clueId), 1));
                            if(CollectionUtils.isEmpty(userIdList)){
                                // 打出日志 方便调试
                                LOG.warn("com.xbongbong.pro.message.help.PushHelper.getClueUserIdList userIdList is empty。corpid:{},cluid:{}",corpid,clueId);
                            }
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            userIdList.addAll(clueUserModel.getClueUserIds(corpid, Collections.singletonList(clueId), 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据客户自定义推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getCustomerRemindUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        //客户自定义模块的推送走轮询，不在这里处理
        return new ArrayList<>();
    }
    /**
     * Description:根据联系人推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getContactUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        ContactEntityExt contactEntity = (ContactEntityExt) entity;

        JSONObject dataObject = JsonHelperUtil.getJSONObject(contactEntity.getData());
        Long customerId = dataObject.getLong(ContactEnum.CUSTOMER_NAME.getAttr());
        CustomerContactPushSet customerContactPushSet = new CustomerContactPushSet();
        PushSetEntity pushSetEntity = customerContactPushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            //只有新数据
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");

            if (customerId != null && customerId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据经销商联系人推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getDistributorContactUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        ContactEntityExt contactEntity = (ContactEntityExt) entity;

        JSONObject dataObject = JsonHelperUtil.getJSONObject(contactEntity.getData());
        Long customerId = dataObject.getLong(DistributorContactEnum.DISTRIBUTOR_NAME.getAttr());
        DistributorContactPushSet distributorContactPushSet = new DistributorContactPushSet();
        PushSetEntity pushSetEntity = distributorContactPushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            //只有新数据
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");

            if (customerId != null && customerId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据销售机会推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getOpportunityUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        OpportunityEntityExt opportunityEntity = (OpportunityEntityExt) entity;
        Long opportunityId = opportunityEntity.getId();
        OpportunityPushSet opportunityPushSet = new OpportunityPushSet();
        PushSetEntity pushSetEntity = opportunityPushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (opportunityId != null && opportunityId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(opportunityUserModel.getOpportunityUserIdList(corpid, opportunityId, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(opportunityUserModel.getOpportunityUserIdList(corpid, opportunityId, 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据访客计划推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getCommunicatePlanUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        //访客计划
        //TODO 访客计划目前不做处理，且需要特殊处理，不在这里处理 ,在新建的时候读取访客计划新建消息，并用pushService中的getPushUserIdList方法获取设置的推送角色人员 @date 2018/8/6 9:25  @author 魏荣杰
        /*String pushObjectString;
        CommunicatePlanEntity communicatePlanEntity = (CommunicatePlanEntity) Entity;
        CommunicatePlanPushSet communicatePlanPushSet = new CommunicatePlanPushSet();
        PushSetEntity pushSetEntity = communicatePlanPushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = FastJsonHelper.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");

            //新数据
            JSONArray  typeJsonArray = FastJsonHelper.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.EXECUTOR.getAlias())) {
//                                result.addAll();
                    }
                }
            }
        } else {
            return new ArrayList<>();
        }*/
        return new ArrayList<>();
    }
    /**
     * Description:根据跟进记录推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getCommunicateUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        //跟进记录
        String pushObjectString;
        Set<String> result = new HashSet<>();
        CustomerCommunicateEntityExt customerCommunicateEntity = (CustomerCommunicateEntityExt) entity;
        CommunicatePushSet communicatePushSet = new CommunicatePushSet();
        PushSetEntity pushSetEntity = communicatePushSet.getPushSet(corpid, subType, pushSetModel);
        JSONObject dataObject = JsonHelperUtil.getJSONObject(customerCommunicateEntity.getData());
        Long customerId = dataObject.getLong(CustomerCommunicateEnum.CUSTOMER_NAME.getAttr());

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (customerId != null && customerId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据经销商跟进记录推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getDistributorCommunicateUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        //跟进记录
        String pushObjectString;
        Set<String> result = new HashSet<>();
        CustomerCommunicateEntityExt customerCommunicateEntity = (CustomerCommunicateEntityExt) entity;
        DistributorCommunicatePushSet distributorCommunicatePushSet = new DistributorCommunicatePushSet();
        PushSetEntity pushSetEntity = distributorCommunicatePushSet.getPushSet(corpid, subType, pushSetModel);
        JSONObject dataObject = JsonHelperUtil.getJSONObject(customerCommunicateEntity.getData());
        Long customerId = dataObject.getLong(DistributorCommunicateEnum.DISTRIBUTOR_NAME.getAttr());

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (customerId != null && customerId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据合同推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getContractUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        ContractEntityExt contractEntity = (ContractEntityExt) entity;
        ContractPushSet contractPushSet = new ContractPushSet();
        PushSetEntity pushSetEntity = contractPushSet.getPushSet(corpid, subType, pushSetModel);
        Long contractId = contractEntity.getId();

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (contractId != null && contractId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractEntity.getId(), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractEntity.getId(), 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            JSONObject contractDataObject = JsonHelperUtil.getJSONObject(contractEntity.getData());
                            result.add(contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr()));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据应收款推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getPaymentPlanUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PaymentPlanPushSet paymentPlanPushSet = new PaymentPlanPushSet();
        PushSetEntity pushSetEntity = paymentPlanPushSet.getPushSet(corpid, subType, pushSetModel);
        Long contractId = null;
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_PLAN_ADD.getSubCode(), subType)) {
                PaymentEntityExt paymentEntity = (PaymentEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentEntity.getData());
                contractId = dataObject.getLong(PaymentEnum.CONTRACT.getAttr());
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (contractId != null && contractId > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractId, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractId, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            ContractEntityExt contractEntity = contractModel.getByKey(contractId, corpid);
                            // todo 合同此处可能查不到
                            if (Objects.nonNull(contractEntity)) {
                                JSONObject contractDataObject = JsonHelperUtil.getJSONObject(contractEntity.getData());
                                result.add(contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr()));
                            }

                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据付款计划推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author chenshan
     * @date 2020/3/6 14:20
     * @since v1.0
     */
    private List<String> getPayPlanUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PayPlanPushSet payPlanPushSet = new PayPlanPushSet();
        PushSetEntity pushSetEntity = payPlanPushSet.getPushSet(corpid, subType, pushSetModel);
        Long purchaseId = null;
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAY_PLAN_ADD.getSubCode(), subType)) {
                PayPlanEntityExt payPlanEntity = (PayPlanEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(payPlanEntity.getData());
                purchaseId = dataObject.getLong(PayPlanEnum.LINK_PURCHASE.getAttr());
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (purchaseId != null && purchaseId > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseId, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseId, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.PURCHASE_SIGNING.getAlias())) {
                            PurchaseEntityExt purchaseEntity = purchaseModel.getByKey(purchaseId, corpid);
                            if (Objects.nonNull(purchaseEntity)) {
                                JSONObject purchaseDataObject = JsonHelperUtil.getJSONObject(purchaseEntity.getData());
                                result.add(purchaseDataObject.getString(PurchaseEnum.SIGNER_ID.getAttr()));
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据回款单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getPaymentSheetUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PaymentSheetPushSet paymentSheetPushSet = new PaymentSheetPushSet();
        PushSetEntity  pushSetEntity = paymentSheetPushSet.getPushSet(corpid, subType, pushSetModel);
        List<String> contractIds = new ArrayList<>();
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                JSONArray contractIdArr = dataObject.getJSONArray(PaymentSheetEnum.CONTRACT.getAttr());
                if(!contractIdArr.isEmpty()){
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<ContractEntityExt> contracts = contractModel.findEntitys(param);
                            // todo起线程得，数据可能查不到了
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(contractEntity -> JsonHelperUtil.getJSONObject(contractEntity.getData())).map(contractDataObject -> contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据红冲回款单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getPaymentSheetRedUserIdList(String corpid,Integer subType, UserEntity userEntity, Entity entity) {
        Set<String> result = new HashSet<>();
        PaymentSheetRedPushSet paymentSheetRedPushSet = new PaymentSheetRedPushSet();
        PushSetEntity pushSetEntity = paymentSheetRedPushSet.getPushSet(corpid, subType, pushSetModel);
        List<String> contractIds = new ArrayList<>();
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_RED_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt entityExt = (PaymentSheetEntityExt) entity;
                JSONObject paymentDataObject = JsonHelperUtil.getJSONObject(entityExt.getData());
                JSONArray contractIdArr = paymentDataObject.getJSONArray(PaymentSheetEnum.CONTRACT.getAttr());
                if(!contractIdArr.isEmpty()){
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            String pushObjectString = pushSetEntity.getPushObject();
            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<ContractEntityExt> contracts = contractModel.findEntitys(param);
                            // todo起线程得，数据可能查不到了
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(contractEntity -> JsonHelperUtil.getJSONObject(contractEntity.getData())).map(contractDataObject -> contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据坏账回款单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getPaymentSheetBadUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PaymentSheetBadPushSet paymentSheetBadPushSet = new PaymentSheetBadPushSet();
        PushSetEntity pushSetEntity = paymentSheetBadPushSet.getPushSet(corpid, subType, pushSetModel);
        List<String> contractIds = new ArrayList<>();
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_BAD_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject paymentDataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                JSONArray contractIdArr = paymentDataObject.getJSONArray(PaymentSheetEnum.CONTRACT.getAttr());
                if(!contractIdArr.isEmpty()){
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            pushObjectString = pushSetEntity.getPushObject();

            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<ContractEntityExt> contracts = contractModel.findEntitys(param);
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(contractEntity -> JsonHelperUtil.getJSONObject(contractEntity.getData())).map(contractDataObject -> contractDataObject.getString(ContractEnum.SIGN_PERSON.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 根据预收款推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaymentSheetPrepaymentUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaymentSheetPrepaymentSet paymentSheetPrepaymentSet = new PaymentSheetPrepaymentSet();
        PushSetEntity pushSetEntity = paymentSheetPrepaymentSet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long customerId = null;
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_PREPAYMENT_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                customerId = dataObject.getLong(PaymentSheetEnum.CUSTOMER_ID.getAttr());
            }
            return getCustomerMainuserCollaborator(corpid, pushSetEntity, userEntity, customerId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 根据红冲预收款推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @throws
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaymentSheetRedPrepaymentUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaymentSheetRedPrepaymentSet paymentSheetRedPrepaymentSet = new PaymentSheetRedPrepaymentSet();
        PushSetEntity pushSetEntity = paymentSheetRedPrepaymentSet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long customerId = null;
            if (Objects.equals(PushTypeEnum.PAYMENT_SHEET_RED_PREPAYMENT_ADD.getSubCode(), subType)) {
                PaymentSheetEntityExt paymentSheetEntityExt = (PaymentSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(paymentSheetEntityExt.getData());
                customerId = dataObject.getLong(PaymentSheetEnum.CUSTOMER_ID.getAttr());
            }
            return getCustomerMainuserCollaborator(corpid, pushSetEntity, userEntity, customerId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据发票推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getInvoiceUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        InvoiceEntityExt invoiceEntity = (InvoiceEntityExt) entity;
        InvoicePushSet invoicePushSet = new InvoicePushSet();
        PushSetEntity pushSetEntity = invoicePushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", invoiceEntity.getCorpid());
        params.put("invoiceId", invoiceEntity.getId());
        params.put("del", 0);

        List<InvoiceRelationshipEntity> invoiceRelationshipEntityList = invoiceRelationshipModel.findEntitys(params);
        Set<Long> customerIds = new HashSet<>();
        Set<String> contractIds = new HashSet<>();
        for (InvoiceRelationshipEntity invoiceRelationshipEntity : invoiceRelationshipEntityList) {
            customerIds.add(invoiceRelationshipEntity.getCustomerId());
            if(!Objects.equals(invoiceRelationshipEntity.getContractId(),0L)) {
                contractIds.add(invoiceRelationshipEntity.getContractId().toString());
            }
        }

        //重复操作完全不需要
        /*JSONObject invoiceDataObject = JsonHelperUtil.getJSONObject(invoiceEntity.getData());
        Long customerId = invoiceDataObject.getLong(InvoiceEnum.CUSTOMER_NAME.getAttr());
        if (customerId != null) {
            customerIds.add(customerId);
        }
        JSONArray contractIdArr = invoiceDataObject.getJSONArray(InvoiceEnum.CONTRACT_ID.getAttr());
        if (Objects.isNull(contractIdArr)) {
            contractIdArr = new JSONArray();
        }
        List<String> contractIdsArrays = contractIdArr.toJavaList(String.class);
        if (contractIdsArrays != null && contractIdsArrays.size() > 0) {
            contractIds.addAll(contractIdsArrays);
        }

        JSONArray paymentIdStr = invoiceDataObject.getJSONArray(InvoiceEnum.PAYMENT_ID.getAttr());
        if (Objects.isNull(paymentIdStr)) {
            paymentIdStr = new JSONArray();
        }
        List<String> paymentIds = paymentIdStr.toJavaList(String.class);
        if (paymentIds != null && paymentIds.size() > 0) {
            List<String> contractIdList = paymentModel.getPaymentJsonExtractListByIn(corpid, paymentIds, PaymentEnum.CONTRACT.getAttr());
            contractIds.addAll(contractIdList);
        }*/

        List<Long> customerIdList = new ArrayList<>(customerIds);
        List<String> contractIdList = new ArrayList<>(contractIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1 )) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 1));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 0));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                        if (contractIdList.size() > 0) {
                            result.addAll(contractModel.getContractJsonExtractListByIn(corpid, contractIdList, ContractEnum.SIGN_PERSON.getAttr()));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }

    }

    /**
     * Description:根据进项发票推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getPurchaseInvoiceUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PurchaseInvoiceEntityExt purchaseInvoiceEntity = (PurchaseInvoiceEntityExt) entity;
        PurchaseInvoicePushSet purchaseInvoicePushSet = new PurchaseInvoicePushSet();
        PushSetEntity pushSetEntity = purchaseInvoicePushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", purchaseInvoiceEntity.getCorpid());
        params.put("purchaseInvoiceId", purchaseInvoiceEntity.getId());
        params.put("del", 0);

        List<PurchaseInvoiceRelationshipEntity> purchaseInvoiceRelationshipEntityList = purchaseInvoiceRelationshipModel.findEntitys(params);
        Set<Long> supplierIds = new HashSet<>();
        Set<String> purchaseIds = new HashSet<>();
        for (PurchaseInvoiceRelationshipEntity purchaseInvoiceRelationshipEntity : purchaseInvoiceRelationshipEntityList) {
            supplierIds.add(purchaseInvoiceRelationshipEntity.getSupplierId());
            if(!Objects.equals(purchaseInvoiceRelationshipEntity.getPurchaseId(),0L)) {
                purchaseIds.add(purchaseInvoiceRelationshipEntity.getPurchaseId().toString());
            }
        }

        List<Long> supplierIdList = new ArrayList<>(supplierIds);
        List<String> purchaseIdList = new ArrayList<>(purchaseIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1 )) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 1));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 0));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.PURCHASE_SIGNING.getAlias())) {
                        if (purchaseIdList.size() > 0) {
                            List<String> siginList = purchaseModel.getPurchaseJsonExtractListByIn(corpid, purchaseIdList, PurchaseEnum.SIGNER_ID.getAttr());
                            result.addAll(siginList);
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }

    }
    /**
     * Description:根据红冲发票推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getInvoiceRedUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        InvoiceEntityExt invoiceEntity = (InvoiceEntityExt) entity;
        InvoiceRedPushSet invoiceRedPushSet = new InvoiceRedPushSet();
        PushSetEntity pushSetEntity = invoiceRedPushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", invoiceEntity.getCorpid());
        params.put("invoiceId", invoiceEntity.getId());
        params.put("del", 0);

        List<InvoiceRelationshipEntity> invoiceRelationshipEntityList = invoiceRelationshipModel.findEntitys(params);
        Set<Long> customerIds = new HashSet<>();
        Set<String> contractIds = new HashSet<>();
        for (InvoiceRelationshipEntity invoiceRelationshipEntity : invoiceRelationshipEntityList) {
            customerIds.add(invoiceRelationshipEntity.getCustomerId());
            if(!Objects.equals(invoiceRelationshipEntity.getCustomerId(),0L)) {
                contractIds.add(invoiceRelationshipEntity.getContractId().toString());
            }
        }
        //新版资金推送只需查明细
        /*JSONObject invoiceDataObject = JsonHelperUtil.getJSONObject(invoiceEntity.getData());
        Long customerId = invoiceDataObject.getLong(InvoiceEnum.CUSTOMER_NAME.getAttr());
        if (customerId != null) {
            customerIds.add(customerId);
        }
        JSONArray contractIdArr = invoiceDataObject.getJSONArray(InvoiceEnum.CONTRACT_ID.getAttr());
        if (Objects.isNull(contractIdArr)) {
            contractIdArr = new JSONArray();
        }
        List<String> contractIdsArrays = contractIdArr.toJavaList(String.class);
        if (contractIdsArrays != null && contractIdsArrays.size() > 0) {
            contractIds.addAll(contractIdsArrays);
        }

        JSONArray paymentIdStr = invoiceDataObject.getJSONArray(InvoiceEnum.PAYMENT_ID.getAttr());
        if (Objects.isNull(paymentIdStr)) {
            paymentIdStr = new JSONArray();
        }
        List<String> paymentIds = paymentIdStr.toJavaList(String.class);
        if (paymentIds != null && paymentIds.size() > 0) {
            List<String> contractIdList = paymentModel.getPaymentJsonExtractListByIn(corpid, paymentIds, PaymentEnum.CONTRACT.getAttr());
            contractIds.addAll(contractIdList);
        }
*/
        List<Long> customerIdList = new ArrayList<>(customerIds);
        List<String> contractIdList = new ArrayList<>(contractIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1 )) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 1));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (customerIdList.size() > 0) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, customerIdList, 0));
                        }
                        if (contractIdList.size() > 0) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                        if (contractIdList.size() > 0) {
                            result.addAll(contractModel.getContractJsonExtractListByIn(corpid, contractIdList, ContractEnum.SIGN_PERSON.getAttr()));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:根据红冲发票推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getPurchaseInvoiceRedUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        PurchaseInvoiceEntityExt purchaseInvoiceEntity = (PurchaseInvoiceEntityExt) entity;
        PurchaseInvoiceRedPushSet purchaseInvoiceRedPushSet = new PurchaseInvoiceRedPushSet();
        PushSetEntity pushSetEntity = purchaseInvoiceRedPushSet.getPushSet(corpid, subType, pushSetModel);

        Map<String, Object> params = new HashMap<>(16);
        params.put("corpid", purchaseInvoiceEntity.getCorpid());
        params.put("purchaseInvoiceId", purchaseInvoiceEntity.getId());
        params.put("del", 0);

        List<PurchaseInvoiceRelationshipEntity> purchaseInvoiceRelationshipEntityList = purchaseInvoiceRelationshipModel.findEntitys(params);
        Set<Long> supplierIds = new HashSet<>();
        Set<String> purchaseIds = new HashSet<>();
        for (PurchaseInvoiceRelationshipEntity purchaseInvoiceRelationshipEntity : purchaseInvoiceRelationshipEntityList) {
            supplierIds.add(purchaseInvoiceRelationshipEntity.getSupplierId());
            if(!Objects.equals(purchaseInvoiceRelationshipEntity.getPurchaseId(),0L)) {
                purchaseIds.add(purchaseInvoiceRelationshipEntity.getPurchaseId().toString());
            }
        }

        List<Long> supplierIdList = new ArrayList<>(supplierIds);
        List<String> purchaseIdList = new ArrayList<>(purchaseIds);

        if (Objects.equals(pushSetEntity.getEnable(), 1 )) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushOject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushOject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 1));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        if (supplierIdList.size() > 0) {
                            result.addAll(supplierUserModel.getSupplierUserIdList(corpid, supplierIdList, 0));
                        }
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, purchaseIdList, 0));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CONTRACT_SIGNING.getAlias())) {
                        if (purchaseIdList.size() > 0) {
                            result.addAll(purchaseModel.getPurchaseJsonExtractListByIn(corpid,purchaseIdList, PurchaseEnum.SIGNER_ID.getAttr()));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据签到推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getSignUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        SignInEntity signInEntity = (SignInEntity) entity;
        SignInPushSet signInPushSet = new SignInPushSet();
        PushSetEntity pushSetEntity = signInPushSet.getPushSet(corpid, subType, pushSetModel);

        Long customerId = signInEntity.getCustomerId();
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            if (Objects.nonNull(customerId)) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据工单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getWorkOrderUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        WorkOrderEntity workOrderEntity = (WorkOrderEntity) entity;
        WorkOrderPushSet workOrderPushSet = new WorkOrderPushSet();
        PushSetEntity pushSetEntity = workOrderPushSet.getPushSet(corpid, subType, pushSetModel);

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    JSONObject dataObject = JsonHelperUtil.getJSONObject(workOrderEntity.getData());
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.CREATOR.getAlias())) {
                        result.add(workOrderEntity.getCreatorId());
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        result.addAll(workOrderFlowUserModel.getTeamUserIdList(workOrderEntity.getId(), corpid, 1));
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        result.addAll(workOrderFlowUserModel.getTeamUserIdList(workOrderEntity.getId(), corpid, 0));
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COPYER.getAlias())) {
                        result.addAll(workOrderCcModel.getUserIds(corpid, workOrderEntity.getId()));
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CUSTOMERMAINUSER.getAlias())) {
                        Long customerId = dataObject.getLong(WorkOrderEnum.LINK_CUSTOMER.getAttr());
                        if (customerId != null) {
                            result.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                        }
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.CONTRACTMAINUSER.getAlias())) {
                        Long contractId = dataObject.getLong(WorkOrderEnum.LINK_CONTRACT.getAttr());
                        if (contractId != null) {
                            result.addAll(contractUserModel.getContractUserIdList(corpid, contractId, 1));
                        }
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }
    /**
     * Description:根据工作报告推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/2/18 14:20
     * @since v1.0
     */
    private List<String> getWorkReportUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        //工作报告模块    工作报告设置和工作报告消息推送有重叠 特殊处理  默认推送给抄送人员，并不在设置页回显@author 魏荣杰  @date 2018/8/16 19:58
        WorkReportEntityExt workReportEntity = (WorkReportEntityExt) entity;
        WorkReportPushSet workReportPushSet = new WorkReportPushSet();
        PushSetEntity pushSetEntity = workReportPushSet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.isNull(userEntity)) {
            return new ArrayList<>();
        }
        
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            // 批阅人推送
            Integer type = workReportEntity.getData().getInteger(WorkReportFieldEnum.TYPE.getAttr());
            List<IdNamePropertyPojo> approvedPeopleList  = getApprovedPeopleList(userEntity,type);
            List<String> approvedPeopleUserIds = getPushUserIdListFromApprovedPeople(userEntity, approvedPeopleList );
            result.addAll(approvedPeopleUserIds);
            pushObjectString = pushSetEntity.getPushObject();

            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            param.put("corpid", corpid);
            param.put("reportId", workReportEntity.getId());
            param.put("del", 0);
            List<WorkReportCcEntity> list = workReportCcModel.findEntitys(param);
            Set<String> userIds = new HashSet<>();
            for (WorkReportCcEntity ccEntity : list) {
                userIds.add(ccEntity.getUserId());
            }
            result.addAll(userIds);
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
        } else {
            return new ArrayList<>();
        }
    }

    public List<IdNamePropertyPojo> getApprovedPeopleList(UserEntity userEntity, Integer type){
        if (Objects.isNull(userEntity)) {
            return new ArrayList<>();
        }
        WorkReportSetEntity workReportSetEntity = workReportSetModel.getByType(type,userEntity.getCorpid());
        List<Long> userDeps = JSONArray.parseArray(userEntity.getDepartment(),Long.class);
        JSONArray jsonArray = null;
        if(workReportSetEntity != null){
            jsonArray = workReportSetEntity.getCcUser();
        }
        JSONArray approvedPeople = new JSONArray();
        if(Objects.nonNull(jsonArray)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                JSONArray effectiveDepsArray = Objects.isNull(jsonObject.getJSONArray(BasicConstant.EFFECTIVE_DEPS)) ? new JSONArray() : jsonObject.getJSONArray(BasicConstant.EFFECTIVE_DEPS);
                List<Long> depList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                List<IdNamePropertyPojo> depIdNameProperty = JSONArray.parseArray(effectiveDepsArray.toJSONString(),IdNamePropertyPojo.class);
                if (Objects.nonNull(depIdNameProperty) && depIdNameProperty.size() != 0){
                    for (IdNamePropertyPojo item : depIdNameProperty) {
                        depList.add(Long.valueOf(item.getId()));
                    }
                }
                // 30946 【成功部】工作报告自动推送给所有的主管
                Boolean flag = true;
                for (Long depId : userDeps) {
                    if (depList.contains(depId)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    continue;
                }
                // 抄送部门为空或全公司
                if (depList.isEmpty() || depList.contains(1L)) {
                    JSONArray userJson = jsonObject.getJSONArray(BasicConstant.APPROVED_PEOPLE);
                    approvedPeople.addAll(userJson);
                } else {
                    depList.retainAll(userDeps);
                    if (depList.size()>=1) {
                        JSONArray userJson = jsonObject.getJSONArray(BasicConstant.APPROVED_PEOPLE);
                        approvedPeople.addAll(userJson);
                    }
                }
            }
        }
        List<IdNamePropertyPojo> defaultList = new ArrayList<>();
        for (int i = 0; i < approvedPeople.size(); i++) {
            JSONObject approvedPeopleJSONObject = approvedPeople.getJSONObject(i);
            IdNamePropertyPojo optionalRangeEntity = new IdNamePropertyPojo();
            optionalRangeEntity.setProperty(approvedPeopleJSONObject.getString(BasicConstant.PROPERTY));
            optionalRangeEntity.setId(approvedPeopleJSONObject.getString(BasicConstant.ID));
            optionalRangeEntity.setName(approvedPeopleJSONObject.getString(BasicConstant.NAME));
            defaultList.add(optionalRangeEntity);
        }
        return defaultList;
    }

    /**
     *       [
     *            {
     * 				"id": "6",
     * 				"name": "二套二厅",
     * 				"property": "dept"
     *            },
     *            {
     * 				"id": "1492590759293",
     * 				"name": "反对法",
     * 				"property": "dept"
     *            },
     *            {
     * 				"id": "1",
     * 				"name": "超级管理员",
     * 				"property": "role"
     *            },
     *            {
     * 				"id": "2",
     * 				"name": "老板",
     * 				"property": "role"
     *            },
     *            {
     * 				"id": "12",
     * 				"name": "后裔",
     * 				"property": "user"
     *            },
     *            {
     * 				"id": "1493198968079",
     * 				"name": "何卡特",
     * 				"property": "user"
     *            },
     *            {
     * 				"id": "1493323563674",
     * 				"name": "黄波",
     * 				"property": "user"
     *            },
     *            {
     * 				"id": "1",
     * 				"name": "1级主管",
     * 				"property": "manager"
     *            },
     *            {
     * 				"id": "2",
     * 				"name": "2级主管",
     * 				"property": "manager"
     *            },
     *            {
     * 				"id": "-1",
     * 				"name": "顶级主管",
     * 				"property": "manager"
     *            }
     * 		]
     * @param userEntity
     * @param approvedPeopleList
     * @return 根据批阅人设置获取userIds
     */
    public List<String> getPushUserIdListFromApprovedPeople(UserEntity userEntity, List<IdNamePropertyPojo> approvedPeopleList){
        if (CollectionsUtil.isEmpty(approvedPeopleList) || Objects.isNull(userEntity)) {
            return new ArrayList<>();
        }
        Set<String> userIdSet = new HashSet<>();
        List<Integer> roleIdList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<Long> depIdList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<String> userIdList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<Integer> managerLevelList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

        try{
            for (int i = 0; i < approvedPeopleList.size(); i++) {
                IdNamePropertyPojo idNamePropertyPojo = approvedPeopleList.get(i);
                String property = idNamePropertyPojo.getProperty();
                String id = idNamePropertyPojo.getId();
                if (Objects.equals(property,BasicConstant.ROLE)){
                    roleIdList.add(Integer.valueOf(id));
                } else if (Objects.equals(property,BasicConstant.DYNAMICMANAGER)){
                    managerLevelList.add(Integer.valueOf(id));
                }else if (Objects.equals(property,BasicConstant.USER)){
                    userIdList.add(id);
                }else if (Objects.equals(property,BasicConstant.DEPT)){
                    depIdList.add(Long.decode(id));
                }
            }
        } catch (Exception e){
            LOG.warn("获取批阅人消息推送列表时 解析失败");
        }

        if(roleIdList != null && roleIdList.size() > 0) {
            List<String> roleUserIds = userModel.getUserIdsByRoles(userEntity.getCorpid(), roleIdList);
            userIdSet.addAll(roleUserIds);
        }
        if(depIdList.size() > 0){
            List<String> depUserIds = userModel.getDepUserIdList(userEntity.getCorpid(),depIdList);
            userIdSet.addAll(depUserIds);
        }
        //上级主管
        if(userEntity != null){
            if( managerLevelList != null && managerLevelList.size() > 0){
                // 获取公司的所有
                List<String> depManagerIdList = getWorkReportDepManagerIdList(userEntity, managerLevelList, departmentModel, userDepartmentModel);
                userIdSet.addAll(depManagerIdList);
            }
        }
        if (userIdList != null) {
            userIdSet.addAll(userIdList);
        }
        return new ArrayList<>(userIdSet);
    }

    /**
     * Description:根据报价单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author 刘阳
     * @date 2019/9/3 10:26 AM
     * @since v1.0
     */
    private List<String> getQuotationUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        String pushObjectString;
        Set<String> result = new HashSet<>();
        QuotationEntityExt quotationEntityExt = (QuotationEntityExt) entity;
        QuotationPushSet quotationPushSet = new QuotationPushSet();
        PushSetEntity pushSetEntity = quotationPushSet.getPushSet(corpid, subType, pushSetModel);
        Long quotationId = quotationEntityExt.getId();

        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            pushObjectString = pushSetEntity.getPushObject();
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            if (quotationId != null && quotationId > 0) {
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(quotationUserModel.getQuotationUserIdList(corpid, quotationEntityExt.getId(), 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(quotationUserModel.getQuotationUserIdList(corpid, quotationEntityExt.getId(), 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.QUOTATION_PERSON.getAlias())) {
                            JSONObject quotationDataObject = JsonHelperUtil.getJSONObject(quotationEntityExt.getData());
                            result.add(quotationDataObject.getString(QuotationEnum.QUOTATION_PERSON.getAttr()));
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * Description:
     * @param userEntity 用户实体
     * @param pushObjectStr 推送设置
     * @param typeUserIdList 当前的既定 推送队列Id 列表（需要补充或去重）
     * @param date 设定的时间
     * @return java.util.List<java.lang.String>
     * @author 魏荣杰
     * @date 2019/4/3 18:50
     * @since v1.0
     */
    public List<String> getPushUserIdList(UserEntity userEntity, String pushObjectStr, List<String> typeUserIdList, Integer date) {
        if (Objects.isNull(pushObjectStr) || Objects.isNull(userEntity)) {
            return new ArrayList<>();
        }
        JSONObject pushObject = JSONObject.parseObject(pushObjectStr);

        Set<String> userIdSet = new HashSet<>();
        if (Objects.nonNull(typeUserIdList)) {
            userIdSet.addAll(typeUserIdList);
        }

        //销售简报推送不考虑推送对象类型，只解析指定的角色、部门、指定员工
        //角色处理
        Integer roleEnable = 0;
        List<Integer> roleIdList = new ArrayList<>();
        try {
            JSONObject roleObject = pushObject.getJSONObject("role");
            if (Objects.nonNull(roleObject)) {
                roleEnable = roleObject.getInteger("enable");
                if (Objects.equals(roleEnable, 1)) {
                    JSONArray roleArray = roleObject.getJSONArray("value");
                    for (Object object : roleArray) {
                        JSONObject jsonObject = (JSONObject) object;
                        roleIdList.add(jsonObject.getInteger("id"));
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("获取推送人列表时 role 解析失败");
        }
        if (CollectionsUtil.isNotEmpty(roleIdList)) {
            List<String> roleUserIds = userModel.getUserIdsByRoles(userEntity.getCorpid(), roleIdList);
            userIdSet.addAll(roleUserIds);
        }

        //部门处理
        List<Long> depIdList = new ArrayList<>();
        try {
            JSONObject depJsonObject = pushObject.getJSONObject("dep");
            if (Objects.nonNull(depJsonObject)) {
                Integer enable = depJsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    JSONArray depArray = depJsonObject.getJSONArray("value");
                    for (Object object : depArray) {
                        JSONObject jsonObject = (JSONObject) object;
                        depIdList.add(jsonObject.getLong("id"));
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("获取推送人列表时 dep 解析失败");
        }
        if (CollectionsUtil.isNotEmpty(depIdList)) {
            List<String> depUserIds = userModel.getUserIdsRecursionDep(depIdList, userEntity.getCorpid());
            userIdSet.addAll(depUserIds);
        }

        //上级主管
        Integer managerEnable = 0;
        List<Integer> managerLevelList = new ArrayList<>();
        try {
            JSONObject managerObject = pushObject.getJSONObject("manager");
            if (Objects.nonNull(managerObject)) {
                managerEnable = managerObject.getInteger("enable");
                if (Objects.equals(managerEnable, 1)) {
                    JSONArray managerArray = managerObject.getJSONArray("value");
                    for (Object object : managerArray) {
                        JSONObject jsonObject = (JSONObject) object;
                        managerLevelList.add(jsonObject.getInteger("id"));
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("获取推送人列表时 manager 解析失败");
        }
        if (CollectionsUtil.isNotEmpty(managerLevelList)) {
            // 获取公司的所有
            List<String> depManagerIdList = getDepManagerIdList(userEntity, managerLevelList, departmentModel, userDepartmentModel);
            userIdSet.addAll(depManagerIdList);
        }

        //指定人处理
        Integer userEnable = 0;
        List<String> userIdList = new ArrayList<>();
        try {
            JSONObject userObject = pushObject.getJSONObject("user");
            if (Objects.nonNull(userObject)) {
                userEnable = userObject.getInteger("enable");
                if (Objects.equals(userEnable, 1)) {
                    JSONArray userArray = userObject.getJSONArray("value");
                    for (Object object : userArray) {
                        JSONObject jsonObject = (JSONObject) object;
                        userIdList.add(jsonObject.getString("id"));
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("获取推送人列表时 user 解析失败");
        }
        if (CollectionsUtil.isNotEmpty(userIdList)) {
            userIdSet.addAll(userIdList);
        }

        //多选组件处理
        Integer multiEnable = 0;
        List<String> multiUserIdList = new ArrayList<>();
        List<Long> multiDeptIdList = new ArrayList<>();
        List<Integer> multiRoleIdList = new ArrayList<>();
        try {
            JSONObject multiObject = pushObject.getJSONObject("multi");
            if (Objects.nonNull(multiObject)) {
                multiEnable = multiObject.getInteger("enable");
                if (Objects.equals(multiEnable, 1)) {
                    JSONArray userArray = multiObject.getJSONArray("value");
                    for (Object object : userArray) {
                        JSONObject jsonObject = (JSONObject) object;
                        if (Objects.equals("user", jsonObject.getString("property"))) {
                            multiUserIdList.add(jsonObject.getString("id"));
                        } else if (Objects.equals("dept", jsonObject.getString("property"))) {
                            multiDeptIdList.add(jsonObject.getLong("id"));
                        } else if (Objects.equals("role", jsonObject.getString("property"))) {
                            multiRoleIdList.add(jsonObject.getInteger("id"));
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("获取多选组件时 multi 解析失败");
        }
        if (Objects.equals(multiEnable, 1)) {
            if (CollectionsUtil.isNotEmpty(multiUserIdList)) {
                userIdSet.addAll(multiUserIdList);
            }
            if (CollectionsUtil.isNotEmpty(multiDeptIdList)) {
                userIdSet.addAll(userModel.getUserIdsRecursionDep(multiDeptIdList, userEntity.getCorpid()));
            }
            if (CollectionsUtil.isNotEmpty(multiRoleIdList)) {
                userIdSet.addAll(userModel.getUserIdsByRoles(userEntity.getCorpid(), multiRoleIdList));
            }
        }

        // 钊琦：推送时如果设置的推送名单包括操作人，也推送消息给操作人
        // 小于5分钟不推送给创建人
        if (date != null) {
            if ((date - DateUtil.getInt()) < 300) {
                userIdSet.remove(userEntity.getUserId());
            }
        }
        return new ArrayList<>(userIdSet);
    }

    /**
     * 工作报告主管解析（特殊逻辑，稍微区别于消息推送）
     * @param userEntity 当前用户
     * @param managerLevelList 主管层级列表
     * @param departmentModel 部门业务层
     * @param userDepartmentModel 用户部门关联业务层
     * @return 主管用户Id列表
     * @author chy
     * @time 2016-11-17 上午9:43:21
     */
    private List<String> getWorkReportDepManagerIdList(UserEntity userEntity, List<Integer> managerLevelList,
                                             DepartmentModel departmentModel, UserDepartmentModel userDepartmentModel) {
        Integer maxLevel = 0;
        if(managerLevelList.size() > 0) {
            //获取最大值的部门等级 部门等级解释：假设a的所在的部门层级是 技术部-后端部-java部 ，那么a的一级部门就是 java部，2级部门就是后端部,3级部门就是技术部
            maxLevel = managerLevelList.stream().max(Comparator.comparing(Long::valueOf)).get();
        }

        List<DepartmentEntity> allDepList = new ArrayList<>();
        try {
            allDepList = departmentModel.findAllDepartment(userEntity.getCorpid(), false);
        } catch (Exception e) {
            LOG.error("com.xbongbong.pro.message.help.PushHelper.getWorkReportDepManagerIdList", e);
        }
        Map<Long, DepartmentEntity> allDepMap = new HashMap<>();
        for (DepartmentEntity dep : allDepList) {
            allDepMap.put(dep.getId(), dep);
        }
        //user所在部门
        List<Long> userInDepIds = JSONObject.parseArray(userEntity.getDepartment(), Long.class);
        if (userInDepIds == null) {
            userInDepIds = new ArrayList<>();
        }
        String userId = userEntity.getUserId();
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("userIdIn", Arrays.asList(userId));
        param.put("corpid", userEntity.getCorpid());
        param.put("del", 0);
        List<UserDepartmentEntity> userDepartmentList = userDepartmentModel.findEntitys(param);

        Set<DepartmentEntity> managerDep = new HashSet<>();
        //user是否是部门主管
        if(userInDepIds.size() > 0){
            for (Long deptId : userInDepIds) {
                // 当前员工所在的部门
                DepartmentEntity departmentEntity = allDepMap.get(deptId);
                if (Objects.isNull(departmentEntity)) {
                    continue;
                }
                //判断当前user是否是dept部门主管
                Boolean flag= false;
                for (UserDepartmentEntity userDepartmentEntity : userDepartmentList) {
                    if (Objects.equals(userDepartmentEntity.getUserId(),userId) && Objects.equals(userDepartmentEntity.getDepartmentId(),departmentEntity.getId()) && Objects.equals(userDepartmentEntity.getIsLeader(),1)){
                        flag=true;
                    }
                }

                //key为部门id value表示是否是主管
                //找不到key对应的value、或key对应的value不是true表示非主管
                Set<DepartmentEntity> pointDepSet = null;
                //获取层级managerLevelList对应的所有部门
                if(!flag){
                    for(Integer i = 1 ; i < maxLevel + 1 ; i++){
                        // 当前user非主管的时候，读取一级主管时，取当前部门的主管
                        if(i == 1){
                            //读取所在部门,第一次时，用所在部门初始化pointDepSet
                            pointDepSet = new HashSet<>();
                            for(Long depId : userInDepIds){
                                DepartmentEntity tempDep = allDepMap.get(depId);

                                if(tempDep != null){
                                    pointDepSet.add(tempDep);
                                }
                            }
                            if(managerLevelList.contains(i)){
                                managerDep.addAll(pointDepSet);
                            }
                            continue;
                        }
                        Set<DepartmentEntity> parentDepSet = new HashSet<>();
                        for(DepartmentEntity dep : pointDepSet){
                            DepartmentEntity tempDep = allDepMap.get(dep.getParentId());
                            if(tempDep != null){
                                parentDepSet.add(tempDep);
                            }
                        }
                        pointDepSet = parentDepSet;
                        if(managerLevelList.contains(i)){
                            managerDep.addAll(pointDepSet);
                        }
                    }
                } else {
                    // 部门ID路由
                    List<Long> routDepIds = StringUtil.toLongList(departmentEntity.getDepIdRouter(), "|");
                    int size = routDepIds.size();

                    // 当前user为主管，推送给上一级部门的主管
                    for(Integer levelInt : managerLevelList) {
                        if (levelInt > 0){
                            levelInt++;
                        }
                        // 顶级主管
                        if(levelInt > size || levelInt == -1) {
                            continue;
                        } else if(levelInt == 0) {
                            // 不选择动态负责人
                            continue;
                        } else {
                            // 级别的主管
                            Long depId = routDepIds.get(size - levelInt);
                            DepartmentEntity tempDep = allDepMap.get(depId);
                            managerDep.add(tempDep);
                        }
                    }
                }
            }
        }

        if(managerDep.isEmpty()){
            return new ArrayList<>();
        }

        //获取完managerLevelList对应的所有部门
        List<Long> managerDepIdList = new ArrayList<>();
        for(DepartmentEntity dep : managerDep){
            if (Objects.isNull(dep)) {
                continue;
            }
            managerDepIdList.add(dep.getId());
        }
        List<UserDepartmentEntity> userDepList = new ArrayList<>();
        if (managerDepIdList.size() > 0) {
            Map<String, Object> params = new HashMap<>();
            params.put("corpid", userEntity.getCorpid());
            params.put("departmentIdIn", managerDepIdList);
            params.put("isLeader", 1);
            params.put("del", 0);
            userDepList = userDepartmentModel.findEntitys(params);
        }

        List<String> depManagerIdList = new ArrayList<>();
        for(UserDepartmentEntity userDep : userDepList){
            depManagerIdList.add(userDep.getUserId());
        }
        depManagerIdList.remove(userEntity.getUserId());

        return depManagerIdList;
    }

    /**
     * 主管解析
     * @param userEntity 当前用户
     * @param managerLevelList 主管层级列表
     * @param departmentModel 部门业务层
     * @param userDepartmentModel 用户部门关联业务层
     * @return 主管用户Id列表
     * @author chy
     * @time 2016-11-17 上午9:43:21
     */
    private List<String> getDepManagerIdList(UserEntity userEntity, List<Integer> managerLevelList,
                                             DepartmentModel departmentModel, UserDepartmentModel userDepartmentModel) {
        Integer maxLevel = 0;
        if(managerLevelList.size() > 0) {
            //获取最大值的部门等级 部门等级解释：假设a的所在的部门层级是 技术部-后端部-java部 ，那么a的一级部门就是 java部，2级部门就是后端部,3级部门就是技术部
            maxLevel = managerLevelList.stream().max(Comparator.comparing(Long::valueOf)).get();
        }

        List<DepartmentEntity> allDepList = new ArrayList<>();
        try {
            allDepList = departmentModel.findAllDepartment(userEntity.getCorpid(), false);
        } catch (Exception e) {
            LOG.error("com.xbongbong.pro.message.help.PushHelper.getDepManagerIdList", e);
        }
        Map<Long, DepartmentEntity> allDepMap = new HashMap<>();
        for (DepartmentEntity dep : allDepList) {
            allDepMap.put(dep.getId(), dep);
        }
        //user所在部门
        List<Long> userInDepIds = JSONObject.parseArray(userEntity.getDepartment(), Long.class);
        if (userInDepIds == null) {
            userInDepIds = new ArrayList<>();
        }
        //user是否是部门主管
        //key为部门id value表示是否是主管
        //找不到key对应的value、或key对应的value不是true表示非主管
        Set<DepartmentEntity> pointDepSet = null;
        Set<DepartmentEntity> managerDep = new HashSet<>();
        //获取层级managerLevelList对应的所有部门
        for(Integer i = 1 ; i < maxLevel + 1 ; i++){
            if(i == 1){
                //读取所在部门,第一次时，用所在部门初始化pointDepSet
                pointDepSet = new HashSet<>();
                for(Long depId : userInDepIds){
                    DepartmentEntity tempDep = allDepMap.get(depId);

                    if(tempDep != null){
                        pointDepSet.add(tempDep);
                    }
                }
                if(managerLevelList.contains(i)){
                    managerDep.addAll(pointDepSet);
                }
                continue;
            }
            Set<DepartmentEntity> parentDepSet = new HashSet<>();
            for(DepartmentEntity dep : pointDepSet){
                DepartmentEntity tempDep = allDepMap.get(dep.getParentId());
                if(tempDep != null){
                    parentDepSet.add(tempDep);
                }
            }
            pointDepSet = parentDepSet;
            if(managerLevelList.contains(i)){
                managerDep.addAll(pointDepSet);
            }
        }
        //获取完managerLevelList对应的所有部门
        List<Long> managerDepIdList = new ArrayList<>();
        for(DepartmentEntity dep : managerDep){
            managerDepIdList.add(dep.getId());
        }
        List<UserDepartmentEntity> userDepList = new ArrayList<>();
        if (managerDepIdList.size() > 0) {
            Map<String, Object> params = new HashMap<>();
            params.put("corpid", userEntity.getCorpid());
            params.put("departmentIdIn", managerDepIdList);
            params.put("isLeader", 1);
            params.put("del", 0);
            userDepList = userDepartmentModel.findEntitys(params);
        }

        List<String> depManagerIdList = new ArrayList<>();
        for(UserDepartmentEntity userDep : userDepList){
            depManagerIdList.add(userDep.getUserId());
        }
        depManagerIdList.remove(userEntity.getUserId());

        return depManagerIdList;
    }


    /**
     * 获取客户的主负责人、协同人
     * @param corpid 公司id
     * @param pushSetEntity 推送设置实体
     * @param userEntity 员工实体
     * @param customerId 客户id
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getCustomerMainuserCollaborator(String corpid, PushSetEntity pushSetEntity, UserEntity userEntity, Long customerId) {
        Set<String> userIdList = new HashSet<>();
        String pushObjectString = pushSetEntity.getPushObject();
        if (customerId != null && customerId > 0) {
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 1));
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        userIdList.addAll(customerUserModel.getCustomerUserIdList(corpid, Collections.singletonList(customerId), 0));
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        } else {
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        }
    }

    /**
     * 根据付款单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaySheetUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetPushSet paySheetPushSet = new PaySheetPushSet();
        PushSetEntity pushSetEntity = paySheetPushSet.getPushSet(corpid, subType, pushSetModel);
        return getPaySheetUserIdList(corpid, subType, userEntity, entity, pushSetEntity, PushTypeEnum.PAY_SHEET_ADD);
    }

    /**
     * 根据红冲付款单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaySheetRedUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetRedPushSet paySheetRedPushSet = new PaySheetRedPushSet();
        PushSetEntity pushSetEntity = paySheetRedPushSet.getPushSet(corpid, subType, pushSetModel);
        return getPaySheetUserIdList(corpid, subType, userEntity, entity, pushSetEntity, PushTypeEnum.PAY_SHEET_RED_ADD);
    }

    /**
     * 根据坏账付款单推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaySheetBadUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetRedPushSet paySheetRedPushSet = new PaySheetRedPushSet();
        PushSetEntity pushSetEntity = paySheetRedPushSet.getPushSet(corpid, subType, pushSetModel);
        return getPaySheetUserIdList(corpid, subType, userEntity, entity, pushSetEntity, PushTypeEnum.PAY_SHEET_BAD_ADD);
    }
    /**
     * 根据预付款推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaySheetPrepayUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetPrepaySet paySheetPrepaySet = new PaySheetPrepaySet();
        PushSetEntity pushSetEntity = paySheetPrepaySet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long supplierId = null;
            if (Objects.equals(PushTypeEnum.PAY_SHEET_PREPAY_ADD.getSubCode(), subType)) {
                PayPlanSheetEntityExt payPlanSheetEntityExt = (PayPlanSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(payPlanSheetEntityExt.getData());
                supplierId = dataObject.getLong(PaySheetEnum.LINK_SUPPLIER.getAttr());
            }
            return getSupplierMainuserCollaborator(corpid, pushSetEntity, userEntity, supplierId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 根据红冲预付款推送设置获取推送对象
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaySheetRedPrepayUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity) {
        PaySheetRedPrepaySet paySheetRedPrepaySet = new PaySheetRedPrepaySet();
        PushSetEntity pushSetEntity = paySheetRedPrepaySet.getPushSet(corpid, subType, pushSetModel);
        if (Objects.equals(pushSetEntity.getEnable(), 1)) {
            Long supplierId = null;
            if (Objects.equals(PushTypeEnum.PAY_SHEET_RED_PREPAY_ADD.getSubCode(), subType)) {
                PayPlanSheetEntityExt payPlanSheetEntityExt = (PayPlanSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(payPlanSheetEntityExt.getData());
                supplierId = dataObject.getLong(PaySheetEnum.LINK_SUPPLIER.getAttr());
            }
            return getSupplierMainuserCollaborator(corpid, pushSetEntity, userEntity, supplierId);
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 获取付款单推送设置获取推送对象
     * 注：该方法只获取采购合同的负责人、签订人，及其他自定义人员
     * @param corpid 公司id
     * @param subType PushTypeEnum 的subType
     * @param userEntity 用户实体
     * @param entity 业务实体
     * @param pushSetEntity 消息设置实体
     * @param paySheetPushTypeEnum 推送类型枚举
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getPaySheetUserIdList(String corpid, Integer subType, UserEntity userEntity, Entity entity, PushSetEntity pushSetEntity, PushTypeEnum paySheetPushTypeEnum) {
        if (Objects.equals(pushSetEntity.getEnable(), BasicConstant.ONE)) {
            List<String> contractIds = new ArrayList<>();
            if (Objects.equals(paySheetPushTypeEnum.getSubCode(), subType)) {
                PayPlanSheetEntityExt entityExt = (PayPlanSheetEntityExt) entity;
                JSONObject dataObject = JsonHelperUtil.getJSONObject(entityExt.getData());
                JSONArray contractIdArr = dataObject.getJSONArray(PaySheetEnum.LINK_PURCHASE.getAttr());
                if (!contractIdArr.isEmpty()) {
                    contractIds = contractIdArr.toJavaList(String.class);
                }
            }
            String pushObjectString = pushSetEntity.getPushObject();
            Set<String> result = new HashSet<>();
            if (contractIds != null && contractIds.size() > 0) {
                JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
                String typeString = pushObject.getString("type");
                JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
                for (Object object : typeJsonArray) {
                    JSONObject jsonObject = (JSONObject) object;
                    Integer enable = jsonObject.getInteger("enable");
                    if (Objects.equals(enable, 1)) {
                        String alias = jsonObject.getString("alias");
                        if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, contractIds, 1));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                            result.addAll(purchaseUserModel.getPurchaseUserIdList(corpid, contractIds, 0));
                        }
                        if (Objects.equals(alias, PushObjectTypeEnum.PURCHASE_SIGNING.getAlias())) {
                            Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            param.put("corpid", corpid);
                            param.put("idIn", contractIds);
                            List<PurchaseEntityExt> contracts = purchaseModel.findEntitys(param);
                            // todo起线程得，数据可能查不到了
                            if (Objects.nonNull(contracts) && contracts.size() > 0) {
                                contracts.stream().map(purchaseEntity -> JsonHelperUtil.getJSONObject(purchaseEntity.getData())).map(contractDataObject -> contractDataObject.getString(PurchaseEnum.SIGNER_ID.getAttr())).forEach(result::add);
                            }
                        }
                    }
                }
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            } else {
                return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(result), DateUtil.getInt());
            }
        } else {
            return new ArrayList<>();
        }
    }

    /**
     * 获取供应商的主负责人、协同人
     * @param corpid 公司id
     * @param pushSetEntity 推送设置实体
     * @param userEntity 员工实体
     * @param supplierId 供应商id
     * @return java.util.List<java.lang.String>
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    private List<String> getSupplierMainuserCollaborator(String corpid, PushSetEntity pushSetEntity, UserEntity userEntity, Long supplierId) {
        Set<String> userIdList = new HashSet<>();
        String pushObjectString = pushSetEntity.getPushObject();
        if (supplierId != null && supplierId > 0) {
            JSONObject pushObject = JsonHelperUtil.parseObject(pushObjectString);
            String typeString = pushObject.getString("type");
            JSONArray typeJsonArray = JsonHelperUtil.parseArray(typeString);
            for (Object object : typeJsonArray) {
                JSONObject jsonObject = (JSONObject) object;
                Integer enable = jsonObject.getInteger("enable");
                if (Objects.equals(enable, 1)) {
                    String alias = jsonObject.getString("alias");
                    if (Objects.equals(alias, PushObjectTypeEnum.MAINUSER.getAlias())) {
                        userIdList.addAll(supplierUserModel.getSupplierUserIdList(corpid, Collections.singletonList(supplierId), 1));
                    }
                    if (Objects.equals(alias, PushObjectTypeEnum.COLLABORATOR.getAlias())) {
                        userIdList.addAll(supplierUserModel.getSupplierUserIdList(corpid, Collections.singletonList(supplierId), 0));
                    }
                }
            }
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        } else {
            return getPushUserIdList(userEntity, pushObjectString, new ArrayList<>(userIdList), DateUtil.getInt());
        }
    }

    /**
     * Description 获取经销商推送的人员
     * @param corpid 公司id
     * @return java.util.List<java.lang.String> 用户id
     * @author 魏荣杰
     * @date 2018/8/9 19:59
     * @since v3.26
     * Modified by:
     */
    public List<String> getDistributorUserIdListByPushSetRule(String corpid, long distributorId) {
        List<DistributorAccountEntity> distributorAccounts = distributorAccountModel.getAllDistributorUserList(corpid, distributorId, DistributorRoleEnum.MANAGER.getCode());

        List<String> userIdList = new ArrayList<>();
        for (DistributorAccountEntity distributorAccount : distributorAccounts) {
            userIdList.add(distributorAccount.getId().toString());
        }
        return userIdList;
    }
}
